package com.song.shop.elasticsearch;

import com.song.shop.entity.Shop;
import com.song.shop.entity.litemallCategoryEntity;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.sort.SortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.*;
import org.springframework.data.util.CloseableIterator;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;

/**
 * @author Song Yinghui
 * @date 2018/10/25
 * @Version 1.0
 */

@RestController
public class ElasticSearch {

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate ;

    @GetMapping(value = "elsearch")
    public Object test(){
        String queryKey = "西游";
        BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
        //queryBuilder.must(QueryBuilders.termQuery("name",queryKey));
        queryBuilder.must(QueryBuilders.matchPhraseQuery("name",queryKey));
        NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
        searchQuery.withQuery(queryBuilder)
                .withIndices("litemall_01")
                .withTypes("doc")
                .withSort(SortBuilders.scoreSort().order(SortOrder.ASC))
                .withSort(SortBuilders.fieldSort("id").order(SortOrder.ASC))
                ;
        List<litemallCategoryEntity> lists = elasticsearchTemplate.queryForList(searchQuery.build(), litemallCategoryEntity.class);
        System.out.println(searchQuery.build().getIndices());
        System.out.println("result ---------------"+queryKey);
        lists.forEach(o -> {
            System.out.println(o.toString());
        });
        return lists;
    }

    @GetMapping("/index")
    public String index(){

        Shop shop = new Shop();
        shop.setId("1812180000012");
        shop.setShopName("修改后的测试名字");
        shop.setShopDesc("这是修改后的描述字段");
        shop.setShopDiscount(0.8);
        shop.setShopImgPath("/img/xiaomi.jpg");

        IndexQuery indexQuery  = new IndexQuery();
        indexQuery.setId(shop.getId());
        indexQuery.setObject(shop);
        indexQuery.setIndexName("shop_01");
        indexQuery.setType("shop");
        String index = elasticsearchTemplate.index(indexQuery);

        UpdateQuery updateQuery = new UpdateQuery();

        elasticsearchTemplate.update(updateQuery);
        return "success"+index;
    }

    @GetMapping("/query")
    public Object query(){

        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.must(QueryBuilders.matchQuery("shopName","测试"));
        NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder();
        NativeSearchQuery build = searchQuery.withIndices("shop_01").withQuery(boolQueryBuilder).withTypes("shop").build();
        List<Shop> shops = elasticsearchTemplate.queryForList(build, Shop.class);
        return shops;
    }

    public void test1(){
        //elasticsearchTemplate.stream();
    }
}
